<template>
	<view class="homeLayout pageBg">
		<custom-nav-bar title="推荐"></custom-nav-bar>

		<view class="banner">
			<swiper circular="true" autoplay="true" indicator-dots indicator-color="#fff" indicator-active-color="rgba(255,255,255,0.4)">
				<swiper-item v-for="item in bannerList" :key="item._id">
					<image :src="item.picurl" mode="aspectFill" @click="clickBanner(item)"></image>
				</swiper-item>
			</swiper>
		</view>

		<view class="notice">
			<view class="left">
				<uni-icons type="sound-filled" size="20"></uni-icons>
				<text class="text">公告</text>
			</view>
			<view class="center">
				<swiper vertical="true" autoplay interval="1500" duration="200" circular>
					<swiper-item v-for="item in noticeList" :key="item._id">
						<navigator :url="'/pages/notice/detail/detail?id=' + item._id">{{ item.title }}</navigator>
					</swiper-item>
				</swiper>
			</view>
			<view class="right">
				<uni-icons type="forward" size="20" color="#999"></uni-icons>
			</view>
		</view>

		<view class="select">
			<common-title>
				<template #name>每日推荐</template>
				<template #custom>
					<view class="date">
						<uni-icons type="calendar" size="20"></uni-icons>
						<view class="text">
							<uni-dateformat :date="Date.now()" format="dd日"></uni-dateformat>
						</view>
					</view>
				</template>
			</common-title>

			<view class="content">
				<scroll-view scroll-x="true">
					<view class="box" v-for="item in dayRandomList" :key="item._id" @click="goPreview(item._id)">
						<image :src="item.smallPicurl" mode="aspectFill"></image>
					</view>
				</scroll-view>
			</view>
		</view>

		<view class="theme">
			<common-title>
				<template #name>专题精选</template>
				<template #custom>
					<navigator url="/pages/classify/classify" open-type="reLaunch" class="more">More+</navigator>
				</template>
			</common-title>
			<view class="content">
				<theme-item v-for="(item, index) in clssifyList" :key="item._id" :item="item"></theme-item>
				<theme-item :isMore="true"></theme-item>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import { apiGetBanner, apiGetDayRandom, apiGetNotice, apiGetClssify } from '@/api/apis.js';
import { onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app';
const bannerList = ref([]);
const dayRandomList = ref([]);
const noticeList = ref([]);
const clssifyList = ref([]);

//获取banner
const getBanner = async () => {
	//返回的是一个Promise对象
	let res = await apiGetBanner();
	console.log(res);
	bannerList.value = res.data.slice(0,res.data.length-1);
};
//获取每日推荐随机图片
const getDayRandom = async () => {
	let res = await apiGetDayRandom();
	dayRandomList.value = res.data;
};

//获取公告列表
const getNotice = async () => {
	let res = await apiGetNotice({ select: true });
	noticeList.value = res.data;
};

//获取专题精选分类
const getClssify = async () => {
	let res = await apiGetClssify({ select: true });
	//console.log(res);
	clssifyList.value = res.data;
};

const clickBanner = (item)=>{
	if(item.target == "miniProgram"){
		uni.navigateTo({
			appId:item.appid,
			url:item.url,
			target:item.target
		})
	}else{
		uni.navigateTo({
			url:`/pages/classlist/classlist?${item.url}`
		})
	}
}

//跳转预览界面
const goPreview = (id) => {
	//设置缓存
	uni.setStorageSync('storageClassList', dayRandomList.value);
	//跳转预览界面
	uni.navigateTo({
		url: '/pages/preview/preview?id=' + id
	});
};

//分享朋友
onShareAppMessage((res) => {
	console.log(res);
	return {
		title: '安谊壁纸来了',
		path: '/pages/index/index'
	};
});

//分享朋友圈
onShareTimeline(() => {
	return {
		title: '安谊壁纸来了'
		//imageUrl:bannerList.value[0].picurl
	};
});

getBanner();
getDayRandom();
getNotice();
getClssify();
</script>

<style lang="scss" scoped>
.homeLayout {
	.banner {
		width: 750rpx;
		padding: 30rpx 0;

		swiper {
			width: 750rpx;
			height: 340rpx;

			swiper-item {
				width: 100%;
				height: 100%;
				padding: 0 30rpx;

				image {
					width: 100%;
					height: 100%;
					border-radius: 16rpx;
				}
			}
		}
	}

	.notice {
		width: 690rpx;
		height: 80rpx;
		background: #f9f9f9;
		margin: 0 auto;
		border-radius: 80rpx;
		display: flex;
		line-height: 80rpx;

		.left {
			width: 140rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			//使用穿透修改子组件样式
			:deep() {
				//!important可以提高css样式属性
				.uni-icons {
					color: $brand-theme-color !important;
				}
			}

			.text {
				font-size: 28rpx;
				font-weight: 600;
				color: $brand-theme-color;
			}
		}

		.center {
			flex: 1;

			swiper {
				height: 100%;

				&-item {
					height: 100%;
					font-size: 30rpx;
					color: #666;
					//单行显示...
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
				}
			}
		}

		.right {
			width: 70rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}

	.select {
		padding-top: 50rpx;

		.date {
			display: flex;
			align-items: center;
			color: $brand-theme-color;
			//使用穿透修改子组件样式
			:deep() {
				//!important可以提高css样式属性
				.uni-icons {
					color: $brand-theme-color !important;
				}
			}
			.text {
				margin-left: 5rpx;
				margin-top: 2rpx;
			}
		}

		.content {
			width: 720rpx;
			margin-left: 30rpx;
			margin-top: 30rpx;

			//scroll-view 横向展示不换行的条件 为以下标了1、2 的地方
			scroll-view {
				//1、不换行
				white-space: nowrap;

				.box {
					width: 200rpx;
					height: 430rpx;
					//2、显示为块级元素，才能保证不换行展示
					display: inline-block;
					margin-right: 15rpx;

					image {
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
				}

				.box:last-child {
					margin-right: 30rpx;
				}
			}
		}
	}

	.theme {
		padding: 50rpx 0;

		.more {
			font-size: 32rpx;
			color: #888;
		}

		.content {
			margin-top: 30rpx;
			padding: 0 30rpx;
			display: grid;
			gap: 15rpx;
			grid-template-columns: repeat(3, 1fr);
		}
	}
}
</style>
